# class BaiduApi:
#     def __init__(self):
#         API_KEY = "LnnCKZ915v5q4fx0wdGqxLmS"
#         SECRET_KEY = "23cg1Pkyh7ZiQkWroXoQPltFlcCM2fIL"
#
#     import requests
#     def get_access_token(self):
#         """
#         使用 AK，SK 生成鉴权签名（Access Token）
#         :return: access_token，或是None(如果错误)
#         """
#         url = "https://aip.baidubce.com/oauth/2.0/token"
#         params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
#         return str(requests.post(url, params=params).json().get("access_token"))
#
    # def info(self):
    #     url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" + self.get_access_token()
    #
    #     payload = 'url=http%3A%2F%2Fswyxj6ht8.hb-bkt.clouddn.com%2FFjDKll07JKpo_f7XsuzmXwytV5Cq%3Fe%3D1748479973%26token%3DcIZQnBWJ7ek5iCrE1UnwSg7qBiWaYBLWT5mEcFcX%3AcJyA_tix7sbk15iePUTsqcnAPlo%3D&detect_direction=false&paragraph=false&probability=false&multidirectional_recognize=false'
    #     headers = {
    #         'Content-Type': 'application/x-www-form-urlencoded',
    #         'Accept': 'application/json'
    #     }
    #
    #     response = requests.request("POST", url, headers=headers, data=payload.encode("utf-8"))
    #
    #     print(response.text)
#
#
import requests


class BaiduApi:
    def __init__(self):
        self.API_KEY = "LnnCKZ915v5q4fx0wdGqxLmS"
        self.SECRET_KEY = "23cg1Pkyh7ZiQkWroXoQPltFlcCM2fIL"

    def get_access_token(self):
        """
        使用 AK，SK 生成鉴权签名（Access Token）
        :return: access_token，或是None(如果错误)
        """
        url = "https://aip.baidubce.com/oauth/2.0/token"
        params = {"grant_type": "client_credentials", "client_id": self.API_KEY, "client_secret": self.SECRET_KEY}
        return str(requests.post(url, params=params).json().get("access_token"))
    def info(self, imurl):
        url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=" + self.get_access_token()

        payload = f'url={imurl}'
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Accept': 'application/json'
        }

        response = requests.request("POST", url, headers=headers, data=payload.encode("utf-8"))
        return response.text

# 删除测试代码
# from urllib.parse import quote
# b = BaiduApi()
# url = quote("http://swyxj6ht8.hb-bkt.clouddn.com/FoCRLbVv5eo1203Ta941ouc9XIPd?e=1748484282&token=cIZQnBWJ7ek5iCrE1UnwSg7qBiWaYBLWT5mEcFcX:VhY2BQw-wp7_JkxFhV3LTr2H5MM=")
# b.info(url)